package lore.miniwechat.service.impl;

import lore.miniwechat.dao.UserDao;
import lore.miniwechat.dto.PageData;
import lore.miniwechat.entity.User;
import lore.miniwechat.mapper.UserMapper;
import lore.miniwechat.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper mapper;
    
    @Autowired
    UserDao dao;

    public List<User> getUsers() {
        return mapper.selectByExample(null);
    }

    public PageData<User> getUserPageData(Integer page,Integer size) {

        if(page!=null&&page<0){
            page=1;
        }
        if(size!=null&&size<0){
            size=10;
        }

        //查总记录数
        long total = mapper.countByExample(null);

        //查当前页的记录
        List<User> userList = dao.findUserPageData((page - 1) * size, size);

        PageData<User> pageData = new PageData<User>();
        pageData.setData(userList);
        pageData.setTotal(total);
        return pageData;
    }

    @Override
    public void insert(User user) {
        mapper.insertSelective(user);
    }


    //根据id更新
    @Override
    public void update(String uid) {

        User user=mapper.selectByPrimaryKey(uid);
        mapper.updateByPrimaryKey(user);
    }


    //标记删除
    @Override
    public void deleteByPrimaryKey(String uid) {

        User user = mapper.selectByPrimaryKey(uid);
        user.setState(0);
        mapper.updateByPrimaryKey(user);

    }


}
